<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/8/7
 * Time: 14:31
 */

namespace App\HttpController\Admin;

use App\Services\ReponseCodeService;
use App\Services\SupportPersonService;
use EasySwoole\Validate\Validate;

class SupportPerson extends AdminBaseController
{
    /**
     * 客服列表
     * showdoc
     * 创建客服
     * @catalog 后台/客服
     * @title   客服列表
     * @description  客服列表
     * @method GET
     * @url /admin/supportPerson
     * @param limit  是 int 每页条数
     * @param page 是 int  页码
     * @param keyword 否 string  关键词搜索，可以输入手机号、昵称
     * @return_param  code int 状态码(200:成功0:失败400:参数错误)
     * @return_param  page int 页码
     * @return_param  limit int 每页条数
     * @return_param  total int 总条数
     * @remark
     */
    public function lists(){
        $param =  $this->param;
        $valitor = new Validate();
        $valitor->addColumn('limit', 'limit')->required('分页条数缺失')->numeric('分页条数缺失');
        $valitor->addColumn('page', 'page')->required('Page Is Required')->numeric('Page Should Be Numeric');

        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, $valitor->getError()->__toString());
        }
        $supportPersonService = new SupportPersonService();
        $ret = $supportPersonService->lists($param);
        if(is_array( $ret)){
            return   $this->writeJson(ReponseCodeService::CODE_OK,$ret, ReponseCodeService::$phrases[ReponseCodeService::CODE_OK]);
        }
        return   $this->writeJson(  $ret,array(), ReponseCodeService::$phrases[ $ret]);
    }

    /**
     * 创建客服
     * showdoc
     * 创建客服
     * @catalog 后台/客服
     * @title   创建客服
     * @description  创建客服
     * @method POST
     * @url /admin/supportPerson
     * @param app_ids  是 string 绑定的app，支持绑定多个app多个id用,做分割点，例如：1或者1,5
     * @param mobile 是 string 手机号，只能填写数字，限制11位
     * @param password 是 string 密码，6位以上，35位以下
     * @param nickname  是 string 昵称，用于前台显示
     * @param real_name  是 string 真实名字
     * @param head 是 string 头像，请直接上传七牛的哈希值就好，不需要给完整路径，长度限制28，例如：FhIn5eW2Z8PWBs9Von5TZZ52BHql
     * @return_param  code int 状态码(200:成功0:失败400:参数错误)
     * @remark 如果app不存在，会失败，手机号不能重复登记
     */
    public function createSupportPerson(){
        $param =  $this->param;
        $valitor = new Validate();
        $valitor->addColumn('app_ids', 'app_ids')->required('AppIds Are Required');
        $valitor->addColumn('mobile', 'mobile')->required('Mobile Is Required')->numeric('Mobile Should Be Numeric')->betweenLen(8,12);
        $valitor->addColumn('password','password')->required('Password is require')->betweenLen(6,35,'The password can only be set to 6 to 35 digits');
        $valitor->addColumn('realname','realname')->betweenLen(2,45,'Illegal Realname');
        $valitor->addColumn('head','head')->length('Head image format error');
        $valitor->addColumn('status','status')->inArray([0,1,2,3],'Status value is illegal');
        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, $valitor->getError()->__toString());
        }
        $supportPersonService = new SupportPersonService();
        $ret = $supportPersonService->createService($param,$this->adminInfo);
        return   $this->writeJson(  $ret,array(), ReponseCodeService::$phrases[ $ret]);
    }

    /**
     * 编辑客服
     * showdoc
     * 编辑客服
     * @catalog 后台/客服
     * @title   编辑客服
     * @description  编辑客服
     * @method PUT
     * @url /admin/supportPerson
     * @param id  是 int 客服id，必须上传才能正常更新客服信息
     * @param app_ids  否 string 绑定的app，支持绑定多个app多个id用,做分割点，例如：1或者1,5
     * @param mobile 否 string 手机号，只能填写数字，限制11位
     * @param password 否 string 密码，6位以上，35位以下
     * @param nickname  否 string 昵称，用于前台显示
     * @param real_name  否 string 真实名字
     * @param status 否 int 状态：1正常0删除,如果需要删除用户，则传status过来
     * @param head 否 string 头像，请直接上传七牛的哈希值就好，不需要给完整路径，长度限制28，例如：FhIn5eW2Z8PWBs9Von5TZZ52BHql
     * @return_param  code int 状态码(200:成功0:失败400:参数错误)
     * @remark 如果app不存在，会失败，手机号不能重复登记;如果不需要更新的，不需要传；
     */
    public function editSupportPerson(){
        $param =  $this->param;
        $valitor = new Validate();
        $valitor->addColumn('id', 'id')->numeric('Id Should Be Numeric')->required('Id Is Required');
        $valitor->addColumn('mobile', 'mobile')->numeric('Mobile Should Be Numeric')->betweenLen(8,12);
        $valitor->addColumn('password','password')->betweenLen(6,35,'The password can only be set to 6 to 35 digits');
        $valitor->addColumn('realname','realname')->betweenLen(2,45,'Illegal Realname');
        $valitor->addColumn('head','head')->length('Head image format error');
        $valitor->addColumn('status','status')->inArray([0,1,2,3],'Status value is illegal');
        $bool = $valitor->validate($param);
        if(!$bool){
            return $this->writeJson(ReponseCodeService::CODE_BAD_REQUEST, null, $valitor->getError()->__toString());
        }
        $supportPersonService = new SupportPersonService();
        $ret = $supportPersonService->updateService($param,$this->adminInfo);
        return   $this->writeJson(  $ret,array(), ReponseCodeService::$phrases[ $ret]);
    }
}